package com.lxmes.business.service.impl;

import java.util.List;

import com.lxmes.business.domain.CcChukuInfo;
import com.lxmes.business.domain.CcRukuInfo;
import com.lxmes.business.domain.CcRukuJiesuan;
import com.lxmes.business.mapper.CcChukuInfoMapper;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.business.mapper.CcChukuJiesuanMapper;
import com.lxmes.business.domain.CcChukuJiesuan;
import com.lxmes.business.service.ICcChukuJiesuanService;

/**
 * 出库单结算Service业务层处理
 *
 * @author lx
 * @date 2024-07-29
 */
@Service
public class CcChukuJiesuanServiceImpl implements ICcChukuJiesuanService
{
    @Autowired
    private CcChukuJiesuanMapper ccChukuJiesuanMapper;
    @Autowired
    private CcChukuInfoMapper chukuInfoMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询出库单结算
     *
     * @param id 出库单结算主键
     * @return 出库单结算
     */
    @Override
    public CcChukuJiesuan selectCcChukuJiesuanById(Long id)
    {
        return ccChukuJiesuanMapper.selectCcChukuJiesuanById(id);
    }

    /**
     * 查询出库单结算列表
     *
     * @param ccChukuJiesuan 出库单结算
     * @return 出库单结算
     */
    @Override
    public List<CcChukuJiesuan> selectCcChukuJiesuanList(CcChukuJiesuan ccChukuJiesuan)
    {
        return ccChukuJiesuanMapper.selectCcChukuJiesuanList(ccChukuJiesuan);
    }

    /**
     * 新增出库单结算
     *
     * @param ccChukuJiesuan 出库单结算
     * @return 结果
     */
    @Override
    public int insertCcChukuJiesuan(CcChukuJiesuan ccChukuJiesuan)
    {
        int count= ccChukuJiesuanMapper.selectCountChuKuJiesuanByJiesuanMonth(ccChukuJiesuan.getJiesuanMonth());
        if(count>0){
            throw new ServiceException(ccChukuJiesuan.getJiesuanMonth()+"该月份已结算！");
        }
        String number= ccChukuJiesuan.getQzType()+ NewNumber.createNewNumber(ccChukuJiesuanMapper.getNewCode(DateUtils.getDate()));
        CcChukuInfo chukuInfoParam = new CcChukuInfo();
        chukuInfoParam.setJiesuanMonth(ccChukuJiesuan.getJiesuanMonth());
        chukuInfoParam.setCkState(1L);
        chukuInfoParam.setFactoryCode(SecurityUtils.getFactoryCode());
        List<CcChukuInfo> chukuInfoList = chukuInfoMapper.selectCcChukuInfoJieSuanList(chukuInfoParam);
        for (CcChukuInfo chukuInfo : chukuInfoList) {
            CcChukuJiesuan jiesuan=new CcChukuJiesuan();
            BeanUtils.copyProperties(chukuInfo,jiesuan);
            jiesuan.setId(null);
            jiesuan.setJiesuanDanhao(number);
            jiesuan.setJiesuanMonth(ccChukuJiesuan.getJiesuanMonth());
            jiesuan.setCreateBy(SecurityUtils.getUsername());
            jiesuan.setCreateTime(DateUtils.getNowDate());
            jiesuan.setFactoryCode(SecurityUtils.getFactoryCode());
            jiesuan.setFactoryName(SecurityUtils.getFactoryName());
            ccChukuJiesuanMapper.insertCcChukuJiesuan(jiesuan);
            chukuInfo.setJiesuanState("1");
            chukuInfo.setJiesuanMonth(ccChukuJiesuan.getJiesuanMonth());
            chukuInfoMapper.updateCcChukuInfo(chukuInfo);
        }
        return  1;
    }

    /**
     * 修改出库单结算
     *
     * @param ccChukuJiesuan 出库单结算
     * @return 结果
     */
    @Override
    public int updateCcChukuJiesuan(CcChukuJiesuan ccChukuJiesuan)
    {
        ccChukuJiesuan.setUpdateTime(DateUtils.getNowDate());
        return ccChukuJiesuanMapper.updateCcChukuJiesuan(ccChukuJiesuan);
    }

    /**
     * 批量删除出库单结算
     *
     * @param ids 需要删除的出库单结算主键
     * @return 结果
     */
    @Override
    public int deleteCcChukuJiesuanByIds(Long[] ids)
    {
        return ccChukuJiesuanMapper.deleteCcChukuJiesuanByIds(ids);
    }

    @Override
    public int cancelCcChukuJiesuanByJiesuanMonth(String jiesuanMonth) {
        int count= ccChukuJiesuanMapper.selectCountChuKuJiesuanByJiesuanMonth(jiesuanMonth);
        if(count==0){
            throw new ServiceException(jiesuanMonth+"该月份未结算，无需取消！");
        }
        ccChukuJiesuanMapper.deleteCcChukuJiesuanByJiesuanMonth(jiesuanMonth);
        CcChukuInfo chukuInfoParam = new CcChukuInfo();
        chukuInfoParam.setJiesuanMonth(jiesuanMonth);
        List<CcChukuInfo> chukuInfoList = chukuInfoMapper.selectCcChukuInfoJieSuanList(chukuInfoParam);
        for (CcChukuInfo chukuInfo : chukuInfoList) {
            chukuInfo.setJiesuanState("0");
            chukuInfo.setJiesuanMonth(jiesuanMonth);
            chukuInfoMapper.updateCcChukuInfo(chukuInfo);
        }
        return 1;
    }

    /**
     * 删除出库单结算信息
     *
     * @param id 出库单结算主键
     * @return 结果
     */
    @Override
    public int deleteCcChukuJiesuanById(Long id)
    {
        return ccChukuJiesuanMapper.deleteCcChukuJiesuanById(id);
    }
    @Override
    public String imporIteminfo(List<CcChukuJiesuan> ccChukuJiesuanList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ccChukuJiesuanList) || ccChukuJiesuanList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CcChukuJiesuan ccChukuJiesuan : ccChukuJiesuanList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ccChukuJiesuan);
                ccChukuJiesuan.setCreateBy(operName);
                ccChukuJiesuan.setUpdateBy(operName);
                this.insertCcChukuJiesuan(ccChukuJiesuan);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
